//
//  M1MemberCell.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/20.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1MemberCell.h"
#import "M1VipItem.h"
@implementation M1MemberCell

- (void)layoutSubviews{
    [super layoutSubviews];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds      byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight    cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self m1_createUI];
    }
    return self;
}

- (void)setM1_vipArray:(NSArray<M1MemberModel *> *)m1_vipArray{
    _m1_vipArray = m1_vipArray;
    [_m1_collect reloadData];
}

- (void)m1_createUI{
    UICollectionViewFlowLayout *m1_layout = [[UICollectionViewFlowLayout alloc]init];
    m1_layout.minimumLineSpacing = 1;
    m1_layout.minimumInteritemSpacing = 1;
    m1_layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    m1_layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    m1_layout.itemSize = CGSizeMake(174.5, 114.5);
    
    _m1_seletIndex = 0;
    _m1_collect = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:m1_layout];
    _m1_collect.delegate  = self;
    _m1_collect.dataSource = self;
    _m1_collect.backgroundColor = [UIColor whiteColor];
    [self addSubview:_m1_collect];
    [_m1_collect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [_m1_collect registerClass:[M1VipItem class] forCellWithReuseIdentifier:@"M1VipItem"];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _m1_seletIndex = indexPath.item;
    [collectionView reloadData];
    if (_m1_vip) {
        _m1_vip(_m1_vipArray[_m1_seletIndex]);
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == _m1_seletIndex) {
        return CGSizeMake(174.5, 114.5);
    }
    return CGSizeMake(150, 114.5);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _m1_vipArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    M1VipItem *m1_vipItem = [collectionView dequeueReusableCellWithReuseIdentifier:@"M1VipItem" forIndexPath:indexPath];
    m1_vipItem.m1_titleLB.text = _m1_vipArray[indexPath.item].package_title;
    m1_vipItem.m1_priceLB.text = [NSString stringWithFormat:@"¥%d",[_m1_vipArray[indexPath.item].package_apple_price intValue]];
    m1_vipItem.m1_dexLB.text = _m1_vipArray[indexPath.item].package_desc;
    m1_vipItem.m1_bgView.image =indexPath.item==_m1_seletIndex?[UIImage imageNamed:@"vip_s"]:[UIImage imageNamed:@"vip_n"];
    m1_vipItem.m1_defaultView.hidden = _m1_vipArray[indexPath.item].m1_default == 1?NO:YES;
    return m1_vipItem;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
